package com.focusai.efairy.business.dev;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;

import com.focusai.efairy.R;
import com.focusai.efairy.model.dev.DeviceRealTimeDataItem;
import com.focusai.efairy.model.report.DevLineData;
import com.focusai.efairy.model.response.DeviceDetailInfoResponse;
import com.focusai.efairy.utils.PrUtils;
import com.github.mikephil.charting.components.LegendEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.Utils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by luo_guangwen on 2017/8/14.
 */

public class DeviceDrawHelper {

    /**
     * @param realtimeDataList 某一天的数据
     * @param colors           拆线图颜色
     * @param pt               相同通道
     *                         获取每个对应Pt的折线图的所有点数据
     */
    public static DevLineData getDrawerableLineData(Context context, List<DeviceDetailInfoResponse.RealtimeData> realtimeDataList, int[] colors, int pt) {
        if (realtimeDataList == null)
            return null;
        LinkedHashMap<Integer, List<Entry>> entryMap = new LinkedHashMap<>(); //cid为key
        List<Long> date = new ArrayList<>();
        float rate = PrUtils.getRateByPt(pt); //单位换算率
        for (int j = realtimeDataList.size() - 1; j >= 0; j--) {
            DeviceDetailInfoResponse.RealtimeData realtimeData = realtimeDataList.get(j);
            if (realtimeData != null && realtimeData.data != null) {
                for (int i = 0; i < realtimeData.data.size(); i++) {
                    DeviceRealTimeDataItem dataItem = realtimeData.data.get(i);
                    if (dataItem.cid == 0){
                        continue;
                    }
                    if (dataItem.pt == pt) {
                        if (entryMap.containsKey(dataItem.cid)) {
                            List<Entry> entries = entryMap.get(dataItem.cid);
                            entries.add(new Entry(entries.size(), dataItem.rtv * rate));
                        } else {
                            ArrayList<Entry> value = new ArrayList<>();
                            value.add(new Entry(0, PrUtils.getRountByPt(dataItem.rtv * rate,pt)));
                            entryMap.put(dataItem.cid, value);
                        }
                    }
                }
                date.add(realtimeData.ts);
            }
        }
        DevLineData devLineData = new DevLineData(pt);
        ArrayList<ILineDataSet> sets = new ArrayList<>();
        int i = 0;
        List<LegendEntry> list = new ArrayList<>();
        int color;
        LegendEntry legendEntry;
        for (Map.Entry<Integer, List<Entry>> entry : entryMap.entrySet()) {
            color = i < colors.length ? colors[i] : R.color.second_color_blue;
            sets.add(electricityA(context, entry.getValue(), color));
            i++;
            legendEntry = new LegendEntry();  //拆线图显示的label
            legendEntry.label = "通道" + entry.getKey();
            legendEntry.formColor = color;
            list.add(legendEntry);
        }
        devLineData.setLineData(new LineData(sets));
        devLineData.setxValueDateList(date);
        devLineData.setLegendEntryList(list);
        return devLineData;
    }

    public static LineDataSet electricityA(Context context, List<Entry> entries, int color) {
        LineDataSet dataSet = new LineDataSet(entries, "",7.5f);
//        dataSet.setLineWidth(2.5f);//折线的宽度
        dataSet.setDrawValues(false);//不显示数字
        dataSet.setDrawCircles(false);//不显示圆圈
//        d1.setCubicIntensity(0.05f);//设置曲线的圆滑曲度因子
        dataSet.setHighlightEnabled(true);//允许高亮
        dataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);//展示平滑曲线
        //设置折线下部分渐变色
        dataSet.setDrawFilled(false);
        if (Utils.getSDKInt() >= 18) {
            // fill drawable only supported on api level 18 and above
            Drawable drawable = ContextCompat.getDrawable(context, R.drawable.bg_line_orange);
            dataSet.setFillDrawable(drawable);
        } else {
//            dataSet.setFillColor(context.getResources().getColor(R.color.color_line_grid));
            dataSet.setFillColor(context.getResources().getColor(R.color.transparent));
        }
        //绘制折线的颜色
        dataSet.setColor(color);
//        dataSet.enableDashedHighlightLine(10f, 5f, 0f);
        return dataSet;
    }

    /**
     * 获取对应Pt的所有点数字
     */
    public static LinkedHashMap<Integer, List<Entry>> getDrawerableData(List<DeviceDetailInfoResponse.RealtimeData> realtimeDataList, int pt) {
        if (realtimeDataList == null)
            return null;
        LinkedHashMap<Integer, List<Entry>> entryMap = new LinkedHashMap<>();
        int j = 1;
        for (DeviceDetailInfoResponse.RealtimeData realtimeData : realtimeDataList) {
            if (realtimeData != null && realtimeData.data != null) {
                float x = realtimeData.ts;
                for (int i = 0; i < realtimeData.data.size(); i++) {
                    DeviceRealTimeDataItem dataItem = realtimeData.data.get(i);
                    if (dataItem.pt == pt) {
                        if (entryMap.containsKey(dataItem.cid)) {
                            entryMap.get(dataItem.cid).add(new Entry(j++, dataItem.rtv));
                        } else {
                            j = 1;
                            ArrayList<Entry> value = new ArrayList<>();
                            value.add(new Entry(j, dataItem.rtv));
                            entryMap.put(dataItem.cid, value);
                        }
                    }
                }
            }
        }
        return entryMap;
    }

    /**
     * 获取通道数key:pt value:cid
     */
    public static LinkedHashMap<Integer, Integer> getDrawerableDataPtrCount(List<DeviceDetailInfoResponse.RealtimeData> realtimeDataList) {
        if (realtimeDataList == null || realtimeDataList.size() == 0)
            return new LinkedHashMap<>();
        LinkedHashMap<Integer, Integer> entryMap = new LinkedHashMap<>();
        DeviceDetailInfoResponse.RealtimeData realtimeData = realtimeDataList.get(0);
        if (realtimeData != null && realtimeData.data != null && realtimeData.data.size() > 0) {
            for (DeviceRealTimeDataItem item : realtimeData.data) {
                if (item.cid == 0){
                    continue;
                }
                if (!entryMap.containsKey(item.pt)) {
                    entryMap.put(item.pt, item.cid);
                }
            }
        }
        return entryMap;
    }

}
